package com.wandian.news.mvvm.lifetool.medicine;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.JsonObject;
import com.mylib.libcore.data.TTBaseAppApi;
import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.news.bean.LifeMedicineBean;

import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.mylib.libcore.config.AppConfig.TOUTIAO_KEY;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_LIFE_MEDICINE_YC;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_LIFE_MEDICINE_YP;


/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description LifeMedicineActivityViewModel
 **/
public class LifeMedicineActivityViewModel extends BaseViewModel {
    private TTBaseAppApi baseAppApi;
    public boolean isReFlash =true;
    public MutableLiveData<LifeMedicineBean> dataBean =  new MutableLiveData<>();
    public LifeMedicineActivityViewModel(@NonNull Application application) {
        super(application);
        baseAppApi =  RetrofitUtil.getTouTiaoRetrofit().create(TTBaseAppApi.class);
    }

    public void getData(String type, int page, boolean isReFlash,String searchName){
        this.isReFlash = isReFlash;
        showDialog();
        JsonObject jsonObject = new JsonObject();
            HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key",TOUTIAO_KEY);
        hashMap.put("num","20");
        hashMap.put("word",searchName);
        hashMap.put("page",page+"");
        String url = NEWS_LIFE_MEDICINE_YP;
        if(!UIUtils.isEmpty(type) && type.equals("中药大全")) url = NEWS_LIFE_MEDICINE_YC;
        RxResultHelper.getHttpObservableNothing(getLifecycleProvider(), baseAppApi.getTouTiaoList(url,hashMap,jsonObject))
                .subscribe(new Observer<Object>() {

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Object o) {
                        dismissDialog();
                        Log.d("getTouTiaoList-midici", JsonUtil.toJson(o)+"");
                        if(!UIUtils.isEmpty(o)){
                            LifeMedicineBean bean = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),LifeMedicineBean.class);
                            if(!UIUtils.isEmpty(bean) && bean.getCode()==200){
                                dataBean.setValue(bean);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                        ToastUtil.show("网络错误");
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

}
